/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.xuexiang.templateproject.utils.sdkinit

import android.app.Application
import com.xuexiang.templateproject.MyApp
import com.xuexiang.templateproject.core.BaseActivity
import com.xuexiang.templateproject.utils.TokenUtils
import com.xuexiang.xaop.XAOP
import com.xuexiang.xhttp2.XHttpSDK
import com.xuexiang.xpage.PageConfig
import com.xuexiang.xrouter.launcher.XRouter
import com.xuexiang.xui.XUI
import com.xuexiang.xui.utils.XToastUtils
import com.xuexiang.xutil.XUtil
import com.xuexiang.xutil.common.StringUtils

/**
 * X系列基础库初始化
 *
 * @author xuexiang
 * @since 2019-06-30 23:54
 */
object XBasicLibInit {
    /**
     * 初始化基础库SDK
     */
    fun init(application: Application) {
        //工具类
        initXUtil(application)

        //网络请求框架
        initXHttp2(application)

        //页面框架
        initXPage(application)

        //切片框架
        initXAOP(application)

        //UI框架
        initXUI(application)

        //路由框架
        initRouter(application)
    }

    /**
     * 初始化XUtil工具类
     */
    private fun initXUtil(application: Application) {
        XUtil.init(application)
        XUtil.debug(MyApp.isDebug)
        TokenUtils.init(application)
    }

    /**
     * 初始化XHttp2
     */
    private fun initXHttp2(application: Application) {
        //初始化网络请求框架，必须首先执行
        XHttpSDK.init(application)
        //需要调试的时候执行
        if (MyApp.isDebug) {
            XHttpSDK.debug()
        }
        //        XHttpSDK.debug(new CustomLoggingInterceptor()); //设置自定义的日志打印拦截器
        //设置网络请求的全局基础地址
        XHttpSDK.setBaseUrl("https://gitee.com/")
        //        //设置动态参数添加拦截器
//        XHttpSDK.addInterceptor(new CustomDynamicInterceptor());
//        //请求失效校验拦截器
//        XHttpSDK.addInterceptor(new CustomExpiredInterceptor());
    }

    /**
     * 初始化XPage页面框架
     */
    private fun initXPage(application: Application) {
        PageConfig.getInstance()
            .debug(MyApp.isDebug)
            .setContainActivityClazz(BaseActivity::class.java)
            .init(application)
    }

    /**
     * 初始化XAOP
     */
    private fun initXAOP(application: Application) {
        XAOP.init(application)
        XAOP.debug(MyApp.isDebug)
        //设置动态申请权限切片 申请权限被拒绝的事件响应监听
        XAOP.setOnPermissionDeniedListener { permissionsDenied: List<String?>? ->
            XToastUtils.error(
                "权限申请被拒绝:" + StringUtils.listToString(permissionsDenied, ",")
            )
        }
    }

    /**
     * 初始化XUI框架
     */
    private fun initXUI(application: Application) {
        XUI.init(application)
        XUI.debug(MyApp.isDebug)
    }

    /**
     * 初始化路由框架
     */
    private fun initRouter(application: Application) {
        // 这两行必须写在init之前，否则这些配置在init过程中将无效
        if (MyApp.isDebug) {
            XRouter.openLog() // 打印日志
            XRouter.openDebug() // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        XRouter.init(application)
    }
}